package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.Seller;

public class SellerDAO extends BaseDAO<Seller>{

	public void addSeller(Seller address) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_book_seller(");
		sb.append("sellerName,address,tel,phone,pic,description_mc,userId,foodType");
		sb.append(") values (");
		sb.append(":sellerName,:address,:tel,:phone,:pic,:description_mc,:userId,:foodType");
		sb.append(")");
		
		update(sb.toString(), address);
	}
	
	public void updateSeller(Seller address) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_book_seller set ");
		sb.append(" sellerName=:sellerName,address=:address,tel=:tel,phone=:phone,");
		sb.append(" pic=:pic,description_mc=:description_mc,foodType=:foodType");
		sb.append(" where sellerId = :sellerId");
		
		update(sb.toString(), address);
	}
	
	public List<Seller> listAllInfos() throws Exception{
		String sql = "select * from him_book_seller";
		return getList(sql, Seller.class);
	}
	
	public List<Seller> listAllInfos(long userId) throws Exception{
		String sql = "select * from him_book_seller where userId = ?";
		return getList(sql, Seller.class,userId);
	}
	
	public PageModel getPagedSellers(PageModel model,String search,int foodType,long userId) throws Exception{
		StringBuilder querySQL = new StringBuilder(" select * from (select c.* from (select a.*,b.username from him_book_seller a,him_user b where a.userId = b.userId) c union select d.*,'admin' as username from him_book_seller d where d.userId = 0) m");
		StringBuilder countSQL = new StringBuilder(" select count(*) from (select c.* from (select a.*,b.username from him_book_seller a,him_user b where a.userId = b.userId) c union select d.*,'admin' as username from him_book_seller d where d.userId = 0) m");
		
		StringBuilder whereSQL = new StringBuilder(" where (sellerName like ? or address like ? or tel like ? or phone like ?) ");
		List<Object> paramList = new ArrayList<Object>();
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");
		
		if(foodType > 0){
			whereSQL.append(" and (foodType = ? or foodType like ? or foodType like ? or foodType like ?) ");
			paramList.add(foodType);
			paramList.add(foodType+",%");
			paramList.add("%,"+foodType+",%");
			paramList.add("%,"+foodType);
		}
		
		if(userId > 0){
			querySQL = new StringBuilder("select * from (select a.*,b.username from him_book_seller a,him_user b where a.userId = b.userId) c");
			countSQL = new StringBuilder("select count(*) from (select a.*,b.username from him_book_seller a,him_user b where a.userId = b.userId) c");
			whereSQL.append(" and userId = ? ");
			paramList.add(userId);
		}
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, Seller.class);
	}
	
	public Seller getById(long sellerId) throws Exception{
		String sql = "select * from him_book_seller where sellerId = ?";
		return this.getJavaBean(sql, Seller.class, sellerId);
	}
	
	public void delSeller(long sellerId) throws Exception{
		String sql = "delete from him_book_seller where sellerId = ?";
		this.commonUpdate(sql, sellerId);
	}
	
}
